package com.googlecode.flealancemarket.tapestry.pages;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.Employer;
import com.googlecode.flealancemarket.metamodel.Freelancer;
import com.googlecode.flealancemarket.metamodel.User;
import com.googlecode.flealancemarket.service.commands.GetUserByUsername;
import com.googlecode.flealancemarket.service.commands.SaveUser;
import com.googlecode.flealancemarket.tapestry.util.Session;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.ioc.annotations.Inject;

public class Registration {

    private String username;
    private String password;
    private String password2;
    private String email;
    private String userType;
    private String resume;
    private String corporateName;
    private String website;
    private String description;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setUserType(String t){
        userType = t;
    }

    public String getUserType(){
        return userType;
    }

    public String getResume() {
        return resume;
    }

    public void setResume(String resume) {
        this.resume = resume;
    }

    public String getCorporateName() {
        return corporateName;
    }

    public void setCorporateName(String corporateName) {
        this.corporateName = corporateName;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @ApplicationState
    private Session session;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.SaveUser")
    private SaveUser saveUser;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.GetUserByUsername")
    private GetUserByUsername getUser;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.Freelancer")
    private Freelancer freelancer;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.Employer")
    private Employer employer;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.User")
    private User user;

    Object onSubmitFromUserRegistration(){
        getUser.setUsername(getUsername());
        User existing = getUser.execute();
        if(existing!=null){
            session.setMessage(String.format("User %s already exists.", existing.getUsername()));
            return null;
        }

        if(getUserType().equals("employer")){
            employer.setUsername(getUsername());
            employer.setEmail(getEmail());
            employer.setPassword(getPassword());
            employer.setCorporateName(getCorporateName());
            employer.setDescription(getDescription());
            employer.setWebsite(getWebsite());
            saveUser.setUser(employer);
        } else {
            freelancer.setUsername(getUsername());
            freelancer.setEmail(getEmail());
            freelancer.setPassword(getPassword());
            freelancer.setResume(getResume());
            saveUser.setUser(freelancer);
        }
        saveUser.execute();
        session.setMessage("You have registered successfully. You may login now.");
        return Categories.class;
    }
}
